import request from '@/config/axios'

export interface UserVO {
  id?: number
  nickname: string
  phone?: string
  avatar?: string
  gender?: number
  birthday?: string
  location?: string
  bio?: string
  readingLevel?: string
  preferredLanguage?: string
  totalReadingTime?: number
  booksRead?: number
  favoriteCount?: number
  createTime?: string
  updateTime?: string
}

export interface UserStatsVO {
  totalUsers: number
  activeUsers: number
  todayNewUsers: number
}

// 查询用户列表
export const getUserPage = (params: {
  pageNo?: number
  pageSize?: number
  nickname?: string
  phone?: string
  status?: number
}) => {
  // 转换参数名以匹配后端接口
  const backendParams = {
    current: params.pageNo || 1,
    size: params.pageSize || 10,
    keyword: params.nickname || params.phone,
    status: params.status
  }
  return request.get({ url: '/api/admin/users', params: backendParams })
}

// 获取用户详情
export const getUser = (id: number) => {
  return request.get({ url: `/api/admin/users/${id}` })
}

// 更新用户状态
export const updateUserStatus = (id: number, status: number) => {
  return request.put({ url: `/api/admin/users/${id}/status`, params: { status } })
}

// 创建用户
export const createUser = (data: UserVO) => {
  return request.post({ url: '/api/admin/users', data })
}

// 更新用户
export const updateUser = (id: number, data: UserVO) => {
  return request.put({ url: `/api/admin/users/${id}`, data })
}

// 删除用户
export const deleteUser = (id: number) => {
  return request.delete({ url: `/api/admin/users/${id}` })
}

// 获取用户统计数据
export const getUserStats = () => {
  return request.get({ url: '/api/admin/users/stats' })
}